package org.outlook.apps.lunar;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class LunarUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private static final int NUM_OF_LABEL = 91;//49
	private static final int DAY_OF_WEEK = 7;
	private static final int DEB_WEEEK = 14;
	private static final Font BOLD_FONT = new Font("Default", Font.BOLD, 13);
	private static final Font TITLE_FONT = new Font("Default", Font.BOLD, 15);
	JPanel panel = new JPanel(new BorderLayout());
	JPanel panel1 = new JPanel();
	JPanel panel2 = new JPanel(new GridLayout(13, DAY_OF_WEEK));
	JPanel panel3 = new JPanel();
	JLabel[] labels = new JLabel[NUM_OF_LABEL];
	JLabel y_label = new JLabel("年");
	JLabel m_label = new JLabel("月");
	JComboBox com1 = new JComboBox();
	JComboBox com2 = new JComboBox();
	int re_year, re_month;
	int x_size, y_size;
	String year_num;
	Calendar now = Calendar.getInstance(); // 实例化Calendar
	private JLabel lunarYear;

	LunarUI() {
		super("万年历");
		setSize(450, 350);
		x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());
		y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());
		setLocation((x_size - 450) / 2, (y_size - 350) / 2);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		panel1.add(com1);
		panel1.add(y_label);
		panel1.add(com2);
		panel1.add(m_label);
		lunarYear = new JLabel();
		lunarYear.setFont(TITLE_FONT);
		panel1.add(lunarYear);

		for (int i = 0; i < NUM_OF_LABEL; i++) {
			labels[i] = new JLabel("", JLabel.CENTER);// 将显示的字符设置为居中
			panel2.add(labels[i]);
		}
		panel3.add(new Clock(this));
		panel.add(panel1, BorderLayout.NORTH);
		panel.add(panel2, BorderLayout.CENTER);
		panel.add(panel3, BorderLayout.SOUTH);
		panel.setBackground(Color.white);
		panel1.setBackground(Color.white);
		panel2.setBackground(Color.white);
		panel3.setBackground(Color.white);
		Init();
		com1.addActionListener(new ClockAction());
		com2.addActionListener(new ClockAction());

		setContentPane(panel);
		setVisible(true);
		setResizable(false);
	}

	class ClockAction implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			int c_year, c_month, c_week;
			c_year = Integer.parseInt(com1.getSelectedItem().toString()); // 得到当前所选年份
			c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到当前月份，并减1,计算机中的月为0－11
			c_week = use(c_year, c_month); // 调用函数use，得到星期几
			updateLunarYear(c_year);
			Resetday(c_week, c_year, c_month); // 调用函数Resetday
		}
	}

	public void Init() {
		int year, month_num, first_day_num;
		String log[] = { "日", "一", "二", "三", "四", "五", "六" };
		for (int i = 0; i < DAY_OF_WEEK; i++) {
			labels[i].setText(log[i]);
			labels[i].setOpaque(Boolean.TRUE);
			labels[i].setBackground(Color.LIGHT_GRAY);
		}
		for (int i = 0; i < NUM_OF_LABEL; i = i + DEB_WEEEK) {
			labels[i].setForeground(Color.RED); // 将星期日的日期设置为红色
		}
		for (int i = 6; i < NUM_OF_LABEL; i = i + DEB_WEEEK) {
			labels[i].setForeground(Color.RED);// 将星期六的日期设置为绿色
		}
		for (int i = 1; i < 10000; i++) {
			com1.addItem("" + i);
		}
		for (int i = 1; i < 13; i++) {
			com2.addItem("" + i);
		}
		month_num = (int) (now.get(Calendar.MONTH)); // 得到当前时间的月份
		year = (int) (now.get(Calendar.YEAR)); // 得到当前时间的年份
		com1.setSelectedIndex(year - 1); // 设置下拉列表显示为当前年
		com2.setSelectedIndex(month_num); // 设置下拉列表显示为当前月
		updateLunarYear(year);
		first_day_num = use(year, month_num);
		Resetday(first_day_num, year, month_num);
	}

	private void updateLunarYear(int year) {
		lunarYear.setText("\t"+LunarUtil.cyclical(year-1900+36)+ "【"+LunarUtil.animalsYear(year)+"】");
	}

	public int use(int reyear, int remonth) {
		int week_num;
		now.set(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天
		week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期
		return week_num;
	}

	public void Resetday(int week_log, int year_log, int month_log) {
		int month_day_score; // 存储月份的天数
		int count;
		month_day_score = 0;
		count = 1;

		Calendar cal = Calendar.getInstance();
		cal.set(year_log, month_log + 1, 1);
		cal.add(Calendar.MONTH, -1); // 前个月
		month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天

		for (int i = DAY_OF_WEEK; i < NUM_OF_LABEL; i++) { // 初始化标签
			labels[i].setText("");
			labels[i].setForeground(Color.BLACK);
		}
		week_log = week_log + 6; // 将星期数加6，使显示正确
		month_day_score = month_day_score + week_log;
		LunarElement[] elements = LunarUtil.calendar(year_log, month_log);
		for (int i = week_log, j=i, index=0; j < month_day_score; i++, j++, index++, count++) {
			labels[i].setText(count + "");
			if(elements[index].isToday()) {
				labels[i].setFont(BOLD_FONT);
				labels[i].setForeground(Color.YELLOW);
			}
			if(elements[index].isSpecialDay()) {
				if(elements[index].getLunarFestival() != null) {
					labels[i+DAY_OF_WEEK].setForeground(Color.RED);
					labels[i+DAY_OF_WEEK].setText(LunarUtil.getEllipticalExpression(elements[index].getLunarFestival()));
				} else if(elements[index].getSolarFestival() != null) {
					labels[i+DAY_OF_WEEK].setForeground(Color.BLUE);
					labels[i+DAY_OF_WEEK].setText(LunarUtil.getEllipticalExpression(elements[index].getSolarFestival()));
				} else if(elements[index].getSolarTerms() != null) {
					labels[i+DAY_OF_WEEK].setForeground(Color.GREEN);
					labels[i+DAY_OF_WEEK].setText(LunarUtil.getEllipticalExpression(elements[index].getSolarTerms()));
				}
					
			} else
				labels[i+DAY_OF_WEEK].setText(LunarUtil.getChinaDayString(elements[index].getlDay()));
			final String lunarTooltip = LunarUtil.getLunarTooltip(elements[index]);
			labels[i].setToolTipText(lunarTooltip);
			labels[i+DAY_OF_WEEK].setToolTipText(lunarTooltip);
			if((i+1)%DAY_OF_WEEK == 0)
				i += DAY_OF_WEEK;
		}
	}
	
	public static void startup(Object...objs) {
		JFrame.setDefaultLookAndFeelDecorated(true);
		new LunarUI();
	}

	public static void main(String[] args) {
		startup();
	}
}
